﻿using System;
using AMS.DataAccess.Trademark.BO;
using AMS.DataAccess.Trademark.Interfaces;

public partial class Cases_EditCaseTrademark : BaseWorkPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Utils.ParseIntQueryString(Request, "caseid", ref caseId))
        {
            GlobalTransfer.RedirectToErrorPage("参数不正确");
            return;
        }

        if (!IsPostBack)
        {
            DoDataBind();
        }
        else
        {
            LoadPostBackData();
        }
    }

    int caseId;
    void LoadPostBackData()
    {
        hfTrademarkID.Value = Request[hfTrademarkID.UniqueID];
    }

    private void DoDataBind()
    {
        ITrademarkManager mgr = ManagerFactory.CreateTrademarkManager();
        TrademarkInfo trademark = mgr.GetTrademarkByCase(CallContext, caseId);
        if (trademark != null)
        {
            ucTrademark.EditTrademark(trademark);
        }
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        ITrademarkManager mgr = ManagerFactory.CreateTrademarkManager();
        ICaseBasicInfoManager caseMgr = ManagerFactory.CreateCaseBasicInfoManager();
        if (ucTrademark.CurrentAction == BusinessAction.ActionNew)
        {
            CaseBasicInfo caseInfo = caseMgr.GetCaseBasicInfo(CallContext, caseId);
            TrademarkInfo trademark = new TrademarkInfo();
            ucTrademark.GetTrademark(trademark);
            trademark.CustomerID = caseInfo.CustomerID;
            mgr.AddTrademark(CallContext, trademark);

            caseInfo.TrademarkID = trademark.AID;
            caseMgr.UpdateCaseBasicInfo(CallContext, caseInfo);
            ucTrademark.EditTrademark(trademark);
            MsgInfo("添加成功");
        }
        else
        {
            TrademarkInfo trademark = mgr.GetTrademarkByCase(CallContext, caseId);
            if (trademark != null)
            {
                ucTrademark.GetTrademark(trademark);
                mgr.UpdateTrademark(CallContext, trademark);
                MsgInfo("更新成功");
            }
        }
    }

    protected void lkSearchTrademark_Click(object sender, EventArgs e)
    {
        if (!string.IsNullOrEmpty(hfTrademarkID.Value))
        {
            int tmId = int.Parse(hfTrademarkID.Value);
            ICaseBasicInfoManager caseMgr = ManagerFactory.CreateCaseBasicInfoManager();
            CaseBasicInfo caseInfo = caseMgr.GetCaseBasicInfo(CallContext, caseId);
            caseInfo.TrademarkID = tmId;
            caseMgr.UpdateCaseBasicInfo(CallContext, caseInfo);
            DoDataBind();
        }
    }
}
